using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace HKB.Common
{
	public class Fetch
	{
		private static string[] _WebSearchList;
		public static string UserIp
		{
			get
			{
				string text = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
				if (text == null || text == string.Empty)
				{
					text = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
				}
				string result;
				if (!Fetch.IsIp(text))
				{
					result = "Unknown";
				}
				else
				{
					result = text;
				}
				return result;
			}
		}
		public static string UserBrowser
		{
			get
			{
				string text = HttpContext.Current.Request.UserAgent;
				string result;
				if (text == null || text == string.Empty)
				{
					result = "Unknown";
				}
				else
				{
					text = text.ToLower();
					HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser;
					if (text.IndexOf("firefox") >= 0 || text.IndexOf("firebird") >= 0 || text.IndexOf("myie") >= 0 || text.IndexOf("opera") >= 0 || text.IndexOf("netscape") >= 0 || text.IndexOf("msie") >= 0)
					{
						result = browser.Browser + browser.Version;
					}
					else
					{
						result = "Unknown";
					}
				}
				return result;
			}
		}
		public static string CurrentPath
		{
			get
			{
				string text = HttpContext.Current.Request.Path;
				text = text.Substring(0, text.LastIndexOf("/"));
				string result;
				if (text == "/")
				{
					result = string.Empty;
				}
				else
				{
					result = text;
				}
				return result;
			}
		}
		public static string CurrentUrl
		{
			get
			{
				return HttpContext.Current.Request.Url.ToString();
			}
		}
		public static string webCurrentUrl
		{
			get
			{
				return HttpContext.Current.Request.RawUrl.ToString();
			}
		}
		public static string webDomain
		{
			get
			{
				string text = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
				if (HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString() != "80")
				{
					text = text + ":" + HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString();
				}
				if (Fetch.CurrentUrl.IndexOf("http://") == 0)
				{
					text = "http://" + text;
				}
				else
				{
					if (Fetch.CurrentUrl.IndexOf("https://") == 0)
					{
						text = "https://" + text;
					}
				}
				return text;
			}
		}
		public static string Referrer
		{
			get
			{
				Uri urlReferrer = HttpContext.Current.Request.UrlReferrer;
				string result;
				if (urlReferrer == null)
				{
					result = string.Empty;
				}
				else
				{
					result = System.Convert.ToString(urlReferrer);
				}
				return result;
			}
		}
		public static bool IsPostFromAnotherDomain
		{
			get
			{
				bool result;
				if (HttpContext.Current.Request.HttpMethod == "GET")
				{
					result = false;
				}
				else
				{
					string value = HttpContext.Current.Request.Url.Host.ToLower();
					result = (Fetch.Referrer.IndexOf(value) == -1);
				}
				return result;
			}
		}
		public static bool IsGetFromAnotherDomain
		{
			get
			{
				bool result;
				if (HttpContext.Current.Request.HttpMethod == "POST")
				{
					result = false;
				}
				else
				{
					string value = HttpContext.Current.Request.Url.Host.ToLower();
					result = (Fetch.Referrer.IndexOf(value) == -1);
				}
				return result;
			}
		}
		public static bool IsRobots
		{
			get
			{
				return Fetch.IsWebSearch();
			}
		}
		static Fetch()
		{
			Fetch._WebSearchList = new string[]
			{
				"google",
				"isaac",
				"surveybot",
				"baiduspider",
				"yahoo",
				"yisou",
				"3721",
				"qihoo",
				"daqi",
				"ia_archiver",
				"p.arthur",
				"fast-webcrawler",
				"java",
				"microsoft-atl-native",
				"turnitinbot",
				"webgather",
				"sleipnir",
				"msn"
			};
		}
		public static string GetOrPost(string name)
		{
			string text = HttpContext.Current.Request[name];
			return (text == null) ? string.Empty : text.Trim();
		}
		public static string GetOrPost(string name, System.Text.Encoding pagecode)
		{
			string text = HttpContext.Current.Request[name];
			string result;
			if (text == null)
			{
				result = string.Empty;
			}
			else
			{
				result = HttpUtility.UrlDecode(HttpContext.Current.Request[name], pagecode);
			}
			return result;
		}
		public static string Post(string name)
		{
			string text = HttpContext.Current.Request.Form[name];
			return (text == null) ? string.Empty : text.Trim();
		}
		public static string Get(string name)
		{
			string text = HttpContext.Current.Request.QueryString[name];
			return (text == null) ? string.Empty : text.Trim();
		}
		public static string Get(string name, string chkType)
		{
			string text = Fetch.Get(name);
			bool flag = false;
			string text2 = chkType.ToLower();
			if (text2 != null)
			{
				if (text2 == "int")
				{
					if (string.IsNullOrEmpty(text))
					{
						text = string.Empty;
					}
					else
					{
						try
						{
							int.Parse(text);
							flag = true;
						}
						catch
						{
							flag = false;
						}
					}
					goto IL_70;
				}
				if (text2 == "string")
				{
					flag = Fetch.IsSafety(text);
					goto IL_70;
				}
			}
			flag = true;
			IL_70:
			if (!flag)
			{
				throw new System.Exception(Language.Get("Common/ParamErr", name));
			}
			return text;
		}
		public static int GetID(string name)
		{
			string text = Fetch.Get(name);
			int result = 0;
			bool flag = true;
			if (!string.IsNullOrEmpty(text))
			{
				try
				{
					result = int.Parse(text);
					flag = true;
				}
				catch
				{
					flag = false;
				}
			}
			if (!flag)
			{
				throw new System.Exception(Language.Get("Common/ParamErr", name));
			}
			return result;
		}
		public static long Ip2Int(string ip)
		{
			long result;
			if (!Fetch.IsIp(ip))
			{
				result = -1L;
			}
			else
			{
				string[] array = ip.Split(new char[]
				{
					'.'
				});
				long num = long.Parse(array[0]) * 16777216L;
				num += (long)(int.Parse(array[1]) * 65536);
				num += (long)(int.Parse(array[2]) * 256);
				num += (long)int.Parse(array[3]);
				result = num;
			}
			return result;
		}
		public static string GetCookies(string name)
		{
			AppSetings appSetings = new AppSetings();
			string name2 = appSetings.GetConfig("CookiesName").Trim();
			string result;
			if (string.IsNullOrEmpty(name))
			{
				result = string.Empty;
			}
			else
			{
				if (!(name == "Howave_Administrator") && !(name == "Howave_AdminPassword") && !(name == "language"))
				{
					if (HttpContext.Current.Request.Cookies[name2] != null && HttpContext.Current.Request.Cookies[name2].Values[name] != string.Empty)
					{
						string text = HttpContext.Current.Request.Cookies[name2].Values[name];
						try
						{
							if (Fetch.IsSafety(text))
							{
								result = text;
								return result;
							}
							HttpContext.Current.Response.Write("Cookies中含有SQL安全隐患字符！");
							HttpContext.Current.Response.End();
							result = string.Empty;
							return result;
						}
						catch (System.Exception)
						{
							result = string.Empty;
							return result;
						}
					}
					result = string.Empty;
				}
				else
				{
					if (HttpContext.Current.Request.Cookies[name] != null)
					{
						string text = HttpContext.Current.Request.Cookies[name].Value;
						try
						{
							if (Fetch.IsSafety(text))
							{
								result = text;
								return result;
							}
							HttpContext.Current.Response.Write("Cookies中含有SQL安全隐患字符！");
							HttpContext.Current.Response.End();
							result = string.Empty;
							return result;
						}
						catch (System.Exception)
						{
							result = string.Empty;
							return result;
						}
					}
					result = string.Empty;
				}
			}
			return result;
		}
		public static string GetCookies(string name, string key)
		{
			string result;
			if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(key))
			{
				result = string.Empty;
			}
			else
			{
				if (HttpContext.Current.Request.Cookies[name][key] != null)
				{
					string text = HttpContext.Current.Request.Cookies[name][key].ToString();
					if (Fetch.IsSafety(text))
					{
						result = text;
					}
					else
					{
						HttpContext.Current.Response.Write("Cookies中含有SQL安全隐患字符！");
						HttpContext.Current.Response.End();
						result = string.Empty;
					}
				}
				else
				{
					result = string.Empty;
				}
			}
			return result;
		}
		public static string GetDomainName(string SiteHostName)
		{
			string result;
			if (SiteHostName.ToLower() != "localhost" && SiteHostName != "127.0.0.1" && SiteHostName.Contains("."))
			{
				string[] array = new string[]
				{
					".com.cn",
					".org.cn",
					".net.cn",
					".gov.cn",
					"edu.cn",
					".com",
					".net",
					".cn",
					".org",
					".mobi",
					".biz",
					".cc",
					".tv",
					".name",
					".info"
				};
				for (int i = 0; i < array.Length; i++)
				{
					int num = SiteHostName.LastIndexOf(array[i]);
					if (num > 0)
					{
						SiteHostName = SiteHostName.Remove(num);
						int num2 = SiteHostName.LastIndexOf(".");
						string text;
						if (num2 > 0)
						{
							text = SiteHostName.Substring(num2 + 1) + array[i];
						}
						else
						{
							text = SiteHostName + array[i];
						}
						result = text.ToLower();
						return result;
					}
				}
				result = SiteHostName.ToLower();
			}
			else
			{
				result = SiteHostName.ToLower();
			}
			return result;
		}
		public static string GetTruePath(string path)
		{
			string text = HttpContext.Current.Server.MapPath("~/");
			text += path;
			return text.Replace("\\\\", "\\");
		}
		public static bool IsWebSearch()
		{
			string text = HttpContext.Current.Request.UserAgent;
			bool result;
			if (text == null || string.Empty == text)
			{
				result = true;
			}
			else
			{
				text = text.ToLower();
				for (int i = 0; i < Fetch._WebSearchList.Length; i++)
				{
					if (-1 != text.IndexOf(Fetch._WebSearchList[i]))
					{
						result = true;
						return result;
					}
				}
				result = Fetch.UserBrowser.Equals("Unknown");
			}
			return result;
		}
		public static string UrlEncode(string url)
		{
			return HttpContext.Current.Server.UrlEncode(url);
		}
		public static bool IsSafety(string s)
		{
			string text = s.Replace("%20", " ");
			text = text.Replace("\\s", " ");
			string pattern = "select |insert |delete from |count\\(|drop table|update |truncate |asc\\(|mid\\(|char\\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\"|\\'| or ";
			return !Regex.IsMatch(text, pattern, RegexOptions.IgnoreCase);
		}
		public static bool IsIp(string s)
		{
			string pattern = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$";
			return Regex.IsMatch(s, pattern);
		}
		public static void CopyDirectory(string SourceDirectory, string TargetDirectory)
		{
			System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(SourceDirectory);
			System.IO.DirectoryInfo directoryInfo2 = new System.IO.DirectoryInfo(TargetDirectory);
			if (directoryInfo.Exists)
			{
				if (!directoryInfo2.Exists)
				{
					directoryInfo2.Create();
				}
				System.IO.FileInfo[] files = directoryInfo.GetFiles();
				for (int i = 0; i < files.Length; i++)
				{
					System.IO.File.Copy(files[i].FullName, directoryInfo2.FullName + "\\" + files[i].Name, true);
				}
				System.IO.DirectoryInfo[] directories = directoryInfo.GetDirectories();
				for (int j = 0; j < directories.Length; j++)
				{
					Fetch.CopyDirectory(directories[j].FullName, directoryInfo2.FullName + "\\" + directories[j].Name);
				}
			}
		}
		public static void Copy(string sourceDirName, string destDirName)
		{
			if (sourceDirName.Substring(sourceDirName.Length - 1) != "\\")
			{
				sourceDirName += "\\";
			}
			if (destDirName.Substring(destDirName.Length - 1) != "\\")
			{
				destDirName += "\\";
			}
			if (System.IO.Directory.Exists(sourceDirName))
			{
				if (!System.IO.Directory.Exists(destDirName))
				{
					System.IO.Directory.CreateDirectory(destDirName);
				}
				string[] array = System.IO.Directory.GetFiles(sourceDirName);
				for (int i = 0; i < array.Length; i++)
				{
					string text = array[i];
					System.IO.File.Copy(text, destDirName + System.IO.Path.GetFileName(text), true);
				}
				array = System.IO.Directory.GetDirectories(sourceDirName);
				for (int i = 0; i < array.Length; i++)
				{
					string text = array[i];
					Fetch.Copy(text, destDirName + text.Substring(text.LastIndexOf("\\") + 1));
				}
			}
		}
	}
}
